var g_config = {
	allAuthGetted:false,
	haveAuthGetted:false,
	g_GroupId:null,
	g_group:null,
	data1:null,//所有权限
	data2:null//组已经有的权限
};

$(function(){
	changeActiveItem("groupList");
	init();
	initData();
});

function init(){
	var id = location.hash;
	var ids = id.substring(1, id.length).split(',');
	g_config.g_GroupId = ids[0]; 
	g_config.g_group = ids[1];
	if(!heading.resourceId){
		heading.Id = g_config.g_GroupId;
		heading.group = g_config.g_group;
	}
	if(!g_config.g_GroupId){
		g_config.g_GroupId = heading.resourceId;
		g_config.g_group = heading.group;
	}
}
/**
 * 加载组权限数据
 */
function initData(){
	$.getJSON('/fw/auth/authority_findAll.action',function(data){
		if(data && data.rows){
			g_config.allAuthGetted=true;
			g_config.data1=data.rows;
		}
	});
	$.getJSON('/fw/auth/group_getAuthsByGroup.action',{'groupId':g_config.g_GroupId},function(data){
		if(data && data.rows){
			g_config.haveAuthGetted=true;
			g_config.data2=data.rows;
		}
	});
	setTimeout(setData,200);
}
/**
 * 将组权限信息显示于表单
 */
function setData(){
	if(g_config.allAuthGetted && g_config.haveAuthGetted){
		var data1 = g_config.data1,
			data2 = g_config.data2,
			s = '',s1='',
			len1 = data2.length;
	
		for(var i=0,len=data1.length;i<len;i++){
			var flag=0;
			for(var j=0;j<len1;j++){
				if(data2[j].authority == data1[i].authority){
					flag=1;
					break;
				}
			}
			if(!flag)
				s += "<div class='checkbox'><label><input type='checkbox' value='"+ data1[i].authority +"'>"+ data1[i].authority +"</label></div>";
			else
				s1 += "<div class='checkbox'><label><input type='checkbox' value='"+ data1[i].authority +"'>"+ data1[i].authority +"</label></div>";
		}
		$('#lPanel .panel-body').html(s);
		$('#rPanel .panel-body').html(s1);
	}else{
		setTimeout(setData,500);
	}
}

/**
 * 配置一条组权限信息
 */
function toRight(){
	var ct = $($('#rPanel .panel-body')[0]);
	
	$("#lPanel :checkbox[checked]").each(function(){
		$(this).removeAttr('checked');
		$(this).parent().parent().appendTo(ct);
	});
}
/**
 * 移除一条组权限信息
 */
function toLeft(){
	var ct = $($('#lPanel .panel-body')[0]);
	$("#rPanel :checkbox[checked]").each(function(){
		$(this).removeAttr('checked');
		$(this).parent().parent().appendTo(ct);
	});
}
/**
 * 给组配置所有权限
 */
function allToRight(){
	var ct = $($('#rPanel .panel-body')[0]);
	$("#lPanel :checkbox").each(function(){
		$(this).removeAttr('checked');
		$(this).parent().parent().appendTo(ct);
	});
}
/**
 * 移除组的所有权限
 */
function allToLeft(){
	var ct = $($('#lPanel .panel-body')[0]);
	$("#rPanel :checkbox").each(function(){
		$(this).removeAttr('checked');
		$(this).parent().parent().appendTo(ct);
	});
}

/**
 * 保存组权限配置
 */
function save(){
	var ida=[];
	$('#rPanel :checkbox').each(function(){
		ida.push($(this).val());
		
	});
	var ids = ida.join(',');
	if(ids.length == 0){
		$('#alertdiv').savehandle('setmsg',{msg:'组权限不能为空!',timer:2000});
		return;
	}
	$.getJSON(
		'/fw/auth/group_updateGroupAuths.action',
		{'groupId':g_config.g_GroupId,'groupAu':ids},
		function(data){
			if(data.success){
				$('#alertdiv').savehandle('setmsg',{msg:'修改成功!',timer:2000});
			}else{
				$('#alertdiv').savehandle('setmsg',{msg:'修改失败!',timer:2000});
			}
		}
	);
}

/**
 * 重置组权限配置
 */
function resetData(){
	initData();
}
